package cm.tl.pms.util;

import java.util.Collection;
import java.util.Map;
import java.util.TreeMap;

public class HighLightUtil {
    String content;
    Map<String, String> map;

    public HighLightUtil() {
        map = new TreeMap<>();
    }

    public void setContent(String str) {
        content = str;
    }

    public void setKey(String k) {
        if (!map.containsKey(k)) {
            boolean notExists = true;
            for (String s : map.keySet()) {
                if (k.indexOf(s) != -1) {
                    map.remove(s);
                }
                if (s.indexOf(k) != -1) {
                    notExists = false;
                }
            }
            if (notExists) {
                map.put(k, "<a style=\"color:red\">" + k + "</a>");
            }
        }
    }

    public void setKeys(Object... keys) {
        for (Object o : keys) {
            if (o instanceof String) {
                setKey((String) o);
            }
            if (o instanceof Collection) {
                Collection<String> co = (Collection<String>) o;
                for (String oo : co) {
                    setKey(oo);
                }
            }
        }
    }

    public String markHighLight(String ctt, Object[] obj) {
        if (ctt != null) {
            setContent(content);
        }
        if (obj != null && obj.length > 0) {
            setKeys(obj);
        }
        if (this.content == null) {
            return null;
        }
        this.content = this.content.replaceAll("<", "");
        this.content = this.content.replaceAll(">", "");
        for (String k : map.keySet()) {
            String v = map.get(k);
            this.content = this.content.replaceAll(k, v);
        }
        return "<div>" + this.content + "</div>";
    }
}
